package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.LetterQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.SystemNoticeQuery;
import cn.wolfcode.trip.base.service.IMessageService;
import cn.wolfcode.trip.base.service.ISystemNoticeService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/messages")
public class MessageController {

    @Autowired
    private IMessageService messageService;
    @Autowired
    private ISystemNoticeService systemNoticeService;

    @GetMapping
    public JSONResult listMessages(){
        JSONResult jsonResult=new JSONResult();
        User currentUser = UserContext.getCurrentUser();
        //查询用户被点赞数
        Integer upvokeNum=messageService.countUpvokeNum(currentUser.getId());
        //查询评论数
        Integer commentNum=messageService.countCommentNum(currentUser.getId());
        //获取私信数
        Integer letterNum=messageService.countLetterNum(currentUser.getId());
        //系统通知数
        Integer noticeNum=messageService.countNoticeNum(currentUser.getId());
        Map map=new HashMap<>();
        map.put("upvokeNum",upvokeNum);
        map.put("commentNum",commentNum);
        map.put("letterNum",letterNum);
        map.put("noticeNum",noticeNum);
        jsonResult.setResult(map);
        return jsonResult;
    }

    /**
     * 获取当前用户的系统信息
     * @return
     */
    @GetMapping("/systems")
    public PageInfo listSystemsMsg(SystemNoticeQuery qo){
        qo.setUserId(UserContext.getCurrentUser().getId());
        qo.setOrderBy("sn.createTime desc");
        PageInfo pageInfo=systemNoticeService.queryForPage(qo);
        systemNoticeService.updateNoticeState(2,qo.getUserId());
        return pageInfo;
    }

}
